package com.bjsxt.service.impl;

import com.bjsxt.mapper.RoleMapper;
import com.bjsxt.mapper.RoleMenuMapper;
import com.bjsxt.pojo.PageA;
import com.bjsxt.pojo.Role;
import com.bjsxt.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Override
    public PageA<Role> findAllRole(Integer page, Integer rows) {
        Page pg = PageHelper.startPage(page, rows);
        List<Role> roles = roleMapper.selectAllRole();
        PageA<Role> pga = new PageA<>(roles,pg.getTotal());
        return pga;
    }

    @Override
    public Integer addRole(Role role,Integer[] mids) {

        Integer integer = 0;
        try {
            integer = roleMapper.insertRole(role);
            for (Integer mid:mids){
                roleMenuMapper.insertRoleMenu(role.getRid(),mid);
            }
            integer=1;
        } catch (Exception e) {
            throw e;
        }
        return integer;
    }

    @Override
    public List<Integer> findmid(Integer rid) {
        return roleMenuMapper.selectmid(rid);
    }

    @Override
    public Integer changeRole(Role role,Integer[] mids) {
        int a=0;
        try {
            roleMapper.updateRole(role);
            roleMenuMapper.deleteRoleMenu(role.getRid());
            for (Integer mid:mids){
                roleMenuMapper.insertRoleMenu(role.getRid(),mid);
            }
            a=1;
        } catch (Exception e) {
            throw e;
        }
        return a;
    }
}
